package com.longxi.tianjintianbao.ui.presenter.oa;

import android.content.Intent;

import com.longxi.tianjintianbao.AppConst;
import com.longxi.tianjintianbao.MyApplication;
import com.longxi.tianjintianbao.api.ApiRetrofit;
import com.longxi.tianjintianbao.api.response.HttpResult;
import com.longxi.tianjintianbao.exception.ExceptionHandle;
import com.longxi.tianjintianbao.exception.MySubscriber;
import com.longxi.tianjintianbao.listener.SelectReceiptListListener;
import com.longxi.tianjintianbao.model.ReceiptList;
import com.longxi.tianjintianbao.ui.activity.oa.ChangeFormalDetailActivity;
import com.longxi.tianjintianbao.ui.activity.oa.CopeWithDetailActivity;
import com.longxi.tianjintianbao.ui.activity.oa.CostApplyDetailActivity;
import com.longxi.tianjintianbao.ui.adapter.multitype.ReceiptListViewBinder;
import com.longxi.tianjintianbao.ui.base.BaseActivity;
import com.longxi.tianjintianbao.ui.base.BasePresenter;
import com.longxi.tianjintianbao.ui.base.LazyFragment;
import com.longxi.tianjintianbao.ui.view.oa.IReceiptListFgView;
import com.longxi.tianjintianbao.utils.MyUtils;
import com.longxi.tianjintianbao.utils.NListRefresh;

import org.json.JSONException;
import org.json.JSONObject;

import me.drakeet.multitype.Items;
import me.drakeet.multitype.MultiTypeAdapter;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class ReceiptListFgPresenter extends BasePresenter<IReceiptListFgView> {

    private MultiTypeAdapter adapter;
    private Items items;

    private String mType = "";

    public ReceiptListFgPresenter(BaseActivity context, LazyFragment fragment) {
        super(context, fragment);
    }

    public void initAdapter(){
        if(adapter == null){
            adapter = new MultiTypeAdapter();
            /* 注册类型和 View 的对应关系 */
            adapter.register(ReceiptList.class, new ReceiptListViewBinder(listener));
            getView().getRvContent().setAdapter(adapter);
        }
    }

    public void getRepairList(int page, String state, String type){
        mType = type;
        JSONObject object = new JSONObject();
        try {
            object.put("userid", MyApplication.loginUser.userid);
            object.put("state", state);
            object.put("type", type);
            object.put("page", page);
            object.put("rowcount", 10);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        ApiRetrofit.getInstance().getOAList(object)
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(new MySubscriber<HttpResult<ReceiptList>>(mContext) {
            @Override
            public void onError(ExceptionHandle.ResponeThrowable throwable) {
                mContext.hideWaitingDialog();
                MyUtils.showToast(throwable.message);
            }

            @Override
            public void onNext(HttpResult<ReceiptList> result) {
                items = NListRefresh.getInstance().fjData(result,
                    getView().getRefreshLayout(),
                    items,
                    adapter,
                    mFragment);
            }
        });
    }

    SelectReceiptListListener listener = new SelectReceiptListListener() {
        @Override
        public void onSuccess(String buildId, String state) {
            Intent mainIntent;
            switch (mType) {
                case "13":
                    mainIntent = new Intent(mContext, CostApplyDetailActivity.class);
                    mainIntent.putExtra(AppConst.BILLID,buildId);
                    mainIntent.putExtra(AppConst.STATE,state);
                    mContext.startActivityForResult(mainIntent, AppConst.IntentRequstCode.ACTIVITY_COST_APPLY_DETAIL);
                    break;
                case "280":
                    mainIntent = new Intent(mContext, CopeWithDetailActivity.class);
                    mainIntent.putExtra(AppConst.BILLID,buildId);
                    mainIntent.putExtra(AppConst.STATE,state);
                    mContext.startActivityForResult(mainIntent, AppConst.IntentRequstCode.ACTIVITY_COPE_WITH_DETAIL);
                    break;
                case "19":
                    mainIntent = new Intent(mContext, ChangeFormalDetailActivity.class);
                    mainIntent.putExtra(AppConst.BILLID,buildId);
                    mainIntent.putExtra(AppConst.STATE,state);
                    mContext.startActivityForResult(mainIntent, AppConst.IntentRequstCode.ACTIVITY_CHANGE_FORMAL_DETAIL);
                    break;
            }
        }
    };

    public void onDestroy() {
        listener = null;
    }
}
